package com.ts.enterprise.module.user.service;

import com.ts.enterprise.common.consts.Constant;
import com.ts.enterprise.common.utils.JwtUtil;
import com.ts.enterprise.common.utils.RedisUtil;
import com.ts.enterprise.enums.PlatformEnum;
import com.ts.enterprise.enums.TokenTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


/**
 * @author Administrator
 */
@Slf4j
@Service
public class LoginService {

    @Resource
    private UserService userService;

    @Autowired
    private LoginService loginService;

//    @Override
//    public String loginCheckUserBasic(UserBasic userBasic, PlatformEnum platformEnum, LoginDeviceEnum deviceEnum) {
//        if (EnableEnum.disable.getCode().equals(userBasic.getEnable())) {
//            throw new BusinessException(ExceptionEnum.ACCOUNT_LOCKED_EXCEPTION);
//        }
//        Map<String, Object> map = new HashMap<>();
//        map.put("tokenType", TokenTypeEnum.basic.getDesc());
//        map.put("id", userBasic.getId());
//        map.put("nickname", userBasic.getNickname());
//        map.put("platform", platformEnum.getName());
//        map.put("mobile", userBasic.getMobile());
//        UUID uuid = UUID.randomUUID();
//        map.put("uuid", uuid);
//        map.put("loginDevice", deviceEnum.getDesc());
//        Long tokenExpiration = System.currentTimeMillis()/1000 + Constant.TOKEN_EXPIRATION;
//        String tokenKey = getTokenKey(TokenTypeEnum.basic, userBasic.getId(), uuid.toString());
//        setRedisToken(tokenKey, tokenExpiration);
//        map.put("tokenKey", tokenKey);
//        return JwtUtil.encode(map, Constant.SALT_BASIC_USER);
//    }



    public String loginCheckUser(Integer id, String mobile, PlatformEnum platformEnum) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("platform", platformEnum.getName());
        map.put("mobile", mobile);
        UUID uuid = UUID.randomUUID();
        map.put("uuid", uuid);
        Long tokenExpiration = System.currentTimeMillis()/1000 + Constant.TOKEN_EXPIRATION;
        String tokenKey = getTokenKey(TokenTypeEnum.user, id, uuid.toString());
        setRedisToken(tokenKey, tokenExpiration);
        map.put("tokenKey", tokenKey);
        return JwtUtil.encode(map, Constant.SALT_USER);
    }



    public String getTokenKey(TokenTypeEnum tokenTypeEnum, Integer id, String uuid) {
        String desc = tokenTypeEnum.getDesc();
        return Constant.PREFIX_TOKEN + desc+ ":"+ id + ":"+uuid;
    }

    public String setRedisToken(String key, Long tokenExpiration) {
        RedisUtil.set(key,tokenExpiration.toString(),Constant.TOKEN_EXPIRATION);
        return key;
    }

    public void deleteToken(TokenTypeEnum tokenTypeEnum, Integer id, String uuid) {
        String tokenKey = loginService.getTokenKey(tokenTypeEnum, id, uuid);
        RedisUtil.del(tokenKey);
    }
}
